<?php

/**
 * uniBoard API Smiley
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_smiley extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Smiley ID
	 *
	 * @var integer
	 */
	
	public $smiley_id 				= 0;
	
	/**
	 * Smiley Replace
	 *
	 * @var string
	 */
	
	public $smiley_replace			= '';
	
	/**
	 * Smiley Image
	 *
	 * @var string
	 */
	
	public $smiley_image			= '';
	
	/**
	 * Smiley Width
	 *
	 * @var integer
	 */
	
	public $smiley_width			= '';
	
	/**
	 * Smiley Height
	 *
	 * @var integer
	 */
	
	public $smiley_height			= '';
	
	/**
	 * Smiley Name
	 *
	 * @var string
	 */
	
	public $smiley_name			= '';
		
	/**
	 * Smiley in editor
	 *
	 * @var bool
	 */
	
	public $smiley_in_editor		= true;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store smiley id
		$this -> smiley_id 	= (integer) $target_id;
		
		// Autoload If Smiley ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'smileys
			WHERE smiley_id = \'' . $this -> smiley_id . '\'
			LIMIT 1');
			
			// Crawler found
			if ( $smiley_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> smiley_replace 		= $smiley_result['smiley_replace'];
				$this -> smiley_image 			= $smiley_result['smiley_image'];
				$this -> smiley_width 			= $smiley_result['smiley_width'];
				$this -> smiley_height 			= $smiley_result['smiley_height'];
				$this -> smiley_name 			= $smiley_result['smiley_name'];
				$this -> smiley_in_editor 		= $smiley_result['smiley_in_editor'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_smiley_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> smiley_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'smileys');
			
				// Store new smiley id
				$this -> smiley_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'smileys', 'smiley_id = \'' . $this -> smiley_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'editor_data');
			class_cache::flushCache( 'parsing_data');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'smileys', 'smiley_id = \'' . $this -> smiley_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'editor_data');
		class_cache::flushCache( 'parsing_data');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_replace
	 *
	 * @param string $new_value
	 */
	
	function setSmileyReplace( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> smiley_replace = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_smiley_replace_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['smiley_replace'] = $this -> appCore -> db -> strEscape( $this -> smiley_replace);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_smiley_replace_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_image
	 *
	 * @param string $new_value
	 */
	
	function setSmileyImage( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> smiley_image = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_smiley_image_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['smiley_image'] = $this -> appCore -> db -> strEscape( $this -> smiley_image);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_smiley_image_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_width
	 *
	 * @param integer $new_value
	 */
	
	function setSmileyWidth( $new_value){
		
			// Store in object
			$this -> smiley_width = (integer) $new_value;
			
			// Force minimal value
			if ( $this -> smiley_width < 1)
			{
				$this -> smiley_width = 1;
			}
			
			// Add to made changes
			$this -> changes['smiley_width'] = $this -> smiley_width;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_height
	 *
	 * @param integer $new_value
	 */
	
	function setSmileyHeight( $new_value){
		
			// Store in object
			$this -> smiley_height = (integer) $new_value;
			
			// Force minimal value
			if ( $this -> smiley_height < 1)
			{
				$this -> smiley_height = 1;
			}
			
			// Add to made changes
			$this -> changes['smiley_height'] = $this -> smiley_height;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_name
	 *
	 * @param string $new_value
	 */
	
	function setSmileyName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> smiley_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_smiley_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['smiley_name'] = $this -> appCore -> db -> strEscape( $this -> smiley_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_smiley_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set smiley_in_editor
	 *
	 * @param bool $new_value
	 */
	
	function setSmileyInEditor( $new_value){
		
			// Store in object
			$this -> smiley_in_editor = (bool) $new_value;
			
			// Add to made changes
			$this -> changes['smiley_in_editor'] = $this -> smiley_in_editor;
		
	}
	
}